// pages/category/index.js
import {request} from '../../request/index'
// import regeneratorRuntime from "../../lib/runtime.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftData: [],
    rightData: [],
    currentIndex: 0,
    scrollIndex: 0,
  },
  menuData: [],
  getListData(){
    let leftData = [];
    let rightData = [];
    request({
      url: '/categories'
    }).then(res => {
      this.menuData = res.data.message;
      wx.setStorageSync("menuData", {time:Date.now(),data:this.menuData});
      leftData = this.menuData.map(item =>{
        return item.cat_name;
      })
      rightData = this.menuData[0].children;
      this.setData({
        leftData,
        rightData,
        localMenuData: this.menuData,
      })
    })
  },
  viewTap(e){
    let rightData = [];
    this.setData({
      currentIndex: e.currentTarget.dataset.operation
    })
    rightData = this.menuData[this.data.currentIndex].children;
      this.setData({
        rightData,
        scrollIndex: 0
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let localData = wx.getStorageSync("menuData");
    let time = Date.now() - localData.time;
    let menuData = localData.data;
    if(menuData!==undefined && time< 1000 * 100){
      let leftData = [];
      let rightData = []; 
      console.log("申请缓存",time)
      leftData = menuData.map(item =>{
        return item.cat_name;
      })
      rightData = menuData[this.data.currentIndex].children;
      this.setData({
        leftData,
        rightData
      })
    }else{
      this.getListData();
      console.log('发送请求');
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})